<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Servers extends Public_Controller {
	public function __construct()
	{
		parent::Public_Controller();
		$this->config->load('cirpho');
		$this->load->helper('form');
		$this->lang->load('cirpho');
		$this->lang->load('settings');
		$this->load->model('cip_settings_m');
		$this->load->model('cip_servers_m');
		$this->template
			->append_metadata( css('cirpho.css', 'cirpho') )
			->append_metadata( css('basic.css', 'cirpho') )
			->append_metadata( css('basic_ie.css', 'cirpho') )
			->append_metadata(js('jquery/jquery.min.js', 'cirpho'))
			->append_metadata(js('cirpho-frontend.js', 'cirpho'))
			->append_metadata(js('jquery.simplemodal.js', 'cirpho'))
			->set_partial('shortcuts', 'admin/partials/shortcuts');
		
		$this->_path = FCPATH . $this->config->item('files_folder');
		$this->_command_path = $this->config->item('command_folder');
		$this->_rebuild_command_folder = $this->config->item('rebuild_command_folder');
	}
	public function index($key)
	{
		$connect_password = $this->cip_settings_m->get('connect_password');
		if(!($key == $connect_password->value)){
			die();
		}
		
		$minimum_matching_rate = $this->cip_settings_m->get('minimum_matching_rate');
		$decision_matching_rate = $this->cip_settings_m->get('decision_matching_rate');
		$max_width = $this->cip_settings_m->get('max_width');
		$max_height = $this->cip_settings_m->get('max_height');
		
		
		
		if($_POST['rebuild']){
			//echo "sh ".$this->_rebuild_command_folder."rebuild.sh ".substr($this->_path, 0, strlen($this->_path) -1 )." ".$max_width->value;				
			system("sh ".$this->_rebuild_command_folder."rebuild.sh ".substr($this->_path, 0, strlen($this->_path) -1 )." ".$max_width->value);
			//die();
			
			
			$this->session->set_flashdata('success', lang('cirpho_setting_rebuild_success'));
			
		}

		$this->pyrocache->delete_all('modules_m');
		
		$this->template
			->title($this->module_details['name'], lang('cirpho_setting_title'))
			->set('minimum_matching_rate', $minimum_matching_rate)
			->set('decision_matching_rate', $decision_matching_rate)
			->set('max_width', $max_width)
			->set('max_height', $max_height)
			->append_metadata( css('admin/style.css', 'cirpho') )
			->build('servers/form', $this->data);
	}
	public function rebuild(){
		$max_width = $this->cip_settings_m->get('max_width');
		system("sh ".$this->_rebuild_command_folder."rebuild.sh ".substr($this->_path, 0, strlen($this->_path) -1 )." ".$max_width->value. " ".$_REQUEST['timeStamp']);
		if (!$this->input->is_ajax_request()) {
			die();
		}
	}
	
	
	public function checkRebuild(){
		if (!$this->input->is_ajax_request()) {
			die();
		}
		$resultData = array();
		if(file_exists($this->_path.".status")){
			$resulstString = "result".file_get_contents($this->_path.".status", true);
			$resultData['resultString'] = $resulstString;
			$resultData['timeStamp'] = $_REQUEST['timeStamp'];
			$resultData['result'] = stripos($resulstString, $_REQUEST['timeStamp']);
			if($resulstString != null && stripos($resulstString, $_REQUEST['timeStamp'])){
				$resultData['complete'] = true;
			}else{
				$resultData['complete'] = false;
			}
		}else{
			$resultData['complete'] = false;
		}
		echo json_encode($resultData);
	}
}